package com.ruoyi.ruoyiyinzheng.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.common.mybatis.core.page.PageQuery;
import com.ruoyi.common.mybatis.core.page.TableDataInfo;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.ruoyiyinzheng.domain.bo.StDeviceBo;
import com.ruoyi.ruoyiyinzheng.domain.vo.StDeviceVo;
import com.ruoyi.ruoyiyinzheng.domain.StDevice;
import com.ruoyi.ruoyiyinzheng.mapper.StDeviceMapper;
import com.ruoyi.ruoyiyinzheng.service.IStDeviceService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 设备Service业务层处理
 *
 * @author ruoyi
 * @date 2023-09-19
 */
@RequiredArgsConstructor
@Service
public class StDeviceServiceImpl implements IStDeviceService {

    private final StDeviceMapper baseMapper;

    /**
     * 查询设备
     */
    @Override
    public StDeviceVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询设备列表
     */
    @Override
    public TableDataInfo<StDeviceVo> queryPageList(StDeviceBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<StDevice> lqw = buildQueryWrapper(bo);
        Page<StDeviceVo> result = baseMapper.selectStDevicePageList(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询设备列表
     */
    @Override
    public List<StDeviceVo> queryList(StDeviceBo bo) {
        LambdaQueryWrapper<StDevice> lqw = buildQueryWrapper(bo);
        return baseMapper.selectStDeviceList(lqw);
    }

    private LambdaQueryWrapper<StDevice> buildQueryWrapper(StDeviceBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<StDevice> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getCategoryId() != null, StDevice::getCategoryId, bo.getCategoryId());
        lqw.like(StringUtils.isNotBlank(bo.getName()), StDevice::getName, bo.getName());
        lqw.eq(StringUtils.isNotBlank(bo.getCode()), StDevice::getCode, bo.getCode());
        lqw.eq(StringUtils.isNotBlank(bo.getState()), StDevice::getState, bo.getState());
        lqw.eq(StringUtils.isNotBlank(bo.getFactory()), StDevice::getFactory, bo.getFactory());
        lqw.eq(StringUtils.isNotBlank(bo.getModel()), StDevice::getModel, bo.getModel());
        lqw.eq(StringUtils.isNotBlank(bo.getEnable()), StDevice::getEnable, bo.getEnable());
        return lqw;
    }

    /**
     * 新增设备
     */
    @Override
    public Boolean insertByBo(StDeviceBo bo) {
        StDevice add = BeanUtil.toBean(bo, StDevice.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改设备
     */
    @Override
    public Boolean updateByBo(StDeviceBo bo) {
        StDevice update = BeanUtil.toBean(bo, StDevice.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(StDevice entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除设备
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
